<template>
    <div class="songlistbox">
        <!-- 歌单页 -->
        <div v-show="songlistxs">
            <!-- 返回按键，顶部歌单名 -->
            <mytop v-bind:thisname="allname" v-bind:gdm="list"></mytop>

            <!-- 歌单详情 -->
            <div class="listtoubox">
                <div class="listbgimg">
                    <div class="zhezhao"></div>
                    <img v-lazy="imgs.bgimg" alt="">
                </div>
                <div class="listsmallbox">
                    <div class="listleft">
                        <div class="ltitle">
                            <img v-lazy="imgs.bgimg" alt="">
                        </div>
                    </div>
                    <div class="listright">
                        <div class="listtitle">
                            <div class="listtouimg">
                                <img v-lazy="imgs.theimg" alt="">
                            </div>
                            <span>{{imgs.thename}}</span>
                        </div>
                        <div class="xiangqing">{{imgs.thexq}}</div>
                    </div>
                </div>
            </div>

            <ul class="listcontentbox" ref="ul" :style="{height:xzdg==0?undefined:xzdg+'px'}">
                <li v-for="(item,index) in songarr" :key="item.id">
                    <listone v-bind:myindex="index" v-bind:myid="item.id" v-bind:mypeople="item.people" v-bind:myname="item.name" v-bind:zongid="gdid"></listone>
                </li>
                <li class="loading" v-show="!songlistend">
                    <loading></loading>
                </li>
            </ul>
        </div>
        <loading v-show="!songlistxs"></loading>
    </div>
</template>
<script>
import listone from "../small/listone.vue"
import mytop from "../small/movetop.vue"
import loading from "../small/loading.vue"

export default {
    name:"songlist",
    data(){
        return {
            songlistxs:false,
            gdid:0,

            next:1,
            tim:null,
            timer:null,
            timxs:null,
            songlistend:false,

            // 顶部的歌单名
            list:1,
            allname:'',

            // 歌单详情
            imgs:{
                bgimg:'',
                theimg:'',
                thename:'',
                thexq:'',
            },
            // 当前显示的歌曲列表
            songarr:[],
            // 总歌曲列表
            allsong:[],

            // 歌单高度
            xzdg:1,
        }
    },
    components:{
        loading,
        listone,
        mytop
    },
    created(){
        // console.log(this.$route.query)
        this.gdid = this.$route.query.id
        this.getdata()
        // 创建组件的时候回到最开始的地方
        document.documentElement.scrollTop = 0
        document.body.scrollTop = 0
        // 添加滚动事件
        window.addEventListener('scroll', this.listscroll)
        document.body.addEventListener('scroll', this.listscroll)
    },
    methods:{
        getdata(){
            if(this.gdid != 368258371 || this.gdid != '368258371'){
                axios.get(`https://apis.netstart.cn/music/playlist/detail?id=${this.gdid}`).then(
                    res=>{
                        // console.log(res)
                        this.allname = res.data.playlist.name
                        this.imgs.bgimg = res.data.playlist.coverImgUrl
                        this.imgs.theimg = res.data.playlist.creator.avatarUrl
                        this.imgs.thename = res.data.playlist.creator.nickname
                        this.imgs.thexq = res.data.playlist.description
                        this.allsong = res.data.playlist.trackIds
                        let newarr = res.data.playlist.tracks
                        for(let i = 0;i<newarr.length;i++){
                            let obj ={}
                            obj.name = newarr[i].name
                            obj.id = newarr[i].id
                            let lzmz = ''
                            for(let r = 0 ;r<newarr[i].ar.length;r++){
                                if(r == 0){
                                    lzmz = newarr[i].ar[r].name
                                }else{
                                    lzmz += "/"+newarr[i].ar[r].name
                                }
                            }
                            obj.people = lzmz
                            this.songarr.push(obj)
                        }
                        if(this.songarr.length == this.allsong.length){
                            this.songlistend = true
                        }
                        this.songlistxs = true

                        this.gbgd()
                    },
                    err=>{
                        console.log(err)
                    }
                )
            }else{
                // 喜欢的音乐的歌单列表
                axios.get('https://apis.netstart.cn/music/login/status').then(
                    res=>{
                        // console.log(res)
                        axios.get(`https://apis.netstart.cn/music/playlist/detail?id=368258371`).then(
                            res=>{
                                // console.log(res)
                            },
                            err=>{
                                console.log(err)
                            }
                        )
                    },
                    err=>{
                        console.log(err)
                    }
                )
            }
        },
        listscroll(){
            if(this.$refs.ul !== undefined){
                let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
                // console.log(scrollTop)
                //变量windowHeight是可视区的高度
                let windowHeight = document.documentElement.clientHeight || documentbody.clientHeight;
                this.gun = true
                if(scrollTop >= 200){
                    // console.log(this.$refs.ul)
                    if(scrollTop+windowHeight >= 260+this.$refs.ul.scrollHeight-1){
                        clearTimeout(this.timer)
                        this.timer = setTimeout(()=>{
                            this.next += 1
                            // console.log("到了底部");
                            this.gun = false
                            this.getname()
                        },1000)
                    }
                }
            }
        },
        getname(){
            if(this.songlistend == false){
                let number = this.next * 10
                let thearr = []
                if(this.next*10 >= this.allsong.length){
                    // console.log("没了")
                    thearr = this.allsong.slice((this.next-1)*10)
                }else{
                    // console.log("还有")
                    thearr = this.allsong.slice((this.next-1)*10,number)
                }
                let str = ''
                for(let j = 0;j<thearr.length;j++){
                    if(j == 0){
                        str += thearr[j].id
                    }else{
                        str += ","+thearr[j].id
                    }
                }
                // console.log(str)
                axios.get(`https://apis.netstart.cn/music/song/detail?ids=${str}`).then(
                    res=>{
                        let addarr = res.data.songs
                        this.gun = true
                        this.tim = setTimeout(()=>{
                            for(let i = 0;i<addarr.length;i++){
                                let obj ={}
                                obj.name = addarr[i].name
                                obj.id = addarr[i].id
                                obj.people = addarr[i].al.name
                                this.songarr.push(obj)
                            }
                            clearTimeout(this.tim)
                            if(this.next*10 >= this.allsong.length){
                                this.songlistend = true
                            }
                        },800)
                        // console.log(11,res)
                    },
                    err=>{
                        console.log(err)
                    }
                )
            }
        },
        changelist(res){
            // console.log("list",res)
            // this.$emit("send_other",res.id)
            // var ab = []
            // for(let i = 0;i<this.allsong.length;i++){
            //     ab.push(this.allsong[i].id)
            // }
            // console.log(abcdefg)
            // this.$emit("send_list",{
            //     list1:ab,
            //     which:res.which,
            //     what:0
            // })
        },
        gbgd(){
            let windowHeight = document.documentElement.clientHeight || documentbody.clientHeight;
            let uldg = this.$refs.ul.scrollHeight
            if(this.allsong.length<5){
                this.xzdg = windowHeight-(300-40+30+20)
            }else{
                this.xzdg = 0
            }
        }
    }
}
</script>
<style lang="less" scoped>
.top{
    height: 100%;
    width:100%;
}
.songlistbox{
    position: relative;
    height:100%;
    // height:2200px;
}
.listtoubox{
    width:100%;
    height:300px;
    overflow: hidden;
    position: relative;
    .listbgimg{
        width: 100%;
        position: relative;
        img{
            width: 100%;
        }
    }
    .zhezhao{
        width: 100%;
        height:300px;
        position: absolute;
        top: 0px;
        left:0px;
        background-color: rgba(0, 0, 0, 0.37);
    }
    .listsmallbox{
        position: absolute;
        width: 100%;
        top: 60%;
        left: 50%;
        transform: translate(-50%,-50%);
        display:flex;
        .listleft{
            width: 50%;
            height:100%;
            border-radius: 10px;
            margin: 20px;
            border:1px solid white;
            font-size: 0px;
            // .ltitle{
                img{
                    width: 100%;
                    border-radius: 10px;
                }
            // }
        }
        .listright{
            margin-top:20px;
            width: 100%;
            .listtitle{
                color: white;
                display: flex;
                align-items: center;
                margin-bottom: 15px;
                width: 100%;
                img{
                    border-radius: 50%;
                    width: 20%;
                }
                span{
                    display:inline-block;
                    margin-left:10px;

                }
            }
            .listtouimg{
                width:20%;
                img{
                    width:100%;
                } 
            }
            .xiangqing{
                color: white;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
                overflow: hidden;
            }
        }

    }
}
.listcontentbox{
    z-index: 100;
    width: 100%;
    // height: 300px;
    background-color: #f8f8f8;
    // background-color: red;
    border-top-left-radius: 40px;
    border-top-right-radius: 40px;
    position: relative;
    padding-top:20px;
    margin-top:-40px;
    min-height:70%;
    padding-bottom: 30px;
}
li{
    padding: 10px;
    box-sizing: border-box;
    margin: 10px auto;
    border-radius: 10px;
    background-color: white;
    width: 90%;
    position: relative;
}
.loading{
    background-color: transparent;
    margin-top: 20px;
}
</style>